﻿//Write a method that return the maximal element in a portion of array of integers starting at given index.
//Using it write another method that sorts an array in ascending / descending order.


using System;
using System.Text;

public class MaximalElementInPortionOfArray
{
    public static void Main()
    {
        int length = int.Parse(Console.ReadLine());
        int[] array = new int[length];
        PopulateArray(array);
        PrintArray(array);
        Sort(array, "ascending");
        Sort(array, "descending");
    }

    private static void Sort(int[] array, string order)
    {
        for (int elemenIndex = 0; elemenIndex < array.Length; elemenIndex++)
        {
            int smallArrayElement = (order == "ascending" ? int.MaxValue : int.MinValue);
            int tempVariable;
            int smallElementIndex = 0;
            for (int i = elemenIndex; i < array.Length; i++)
            {
                if (order == "ascending")
                {
                    if (array[i] < smallArrayElement)
                    {
                        smallArrayElement = array[i];
                        smallElementIndex = i;
                    }
                }
                else
                {
                    if (array[i] > smallArrayElement)
                    {
                        smallArrayElement = array[i];
                        smallElementIndex = i;
                    }
                }
            }

            tempVariable = array[elemenIndex];
            array[elemenIndex] = smallArrayElement;
            array[smallElementIndex] = tempVariable;

            Console.Write(array[elemenIndex] + " ");
        }

        Console.WriteLine();
    }

    private static void PopulateArray(int[] array)
    {
        Random rand = new Random();
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = rand.Next(-99, 100);
        }
    }

    private static void PrintArray(int[] array)
    {
        Console.Write("{");
        for (int i = 0; i < array.Length; i++)
        {
            if (i == array.Length - 1)
            {
                Console.Write("{0}}}", array[i]);
            }
            else
            {
                Console.Write("{0}, ", array[i]);
            }
        }

        Console.WriteLine();
    }
}